package cd.itcast.jdbc.day4.factory;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

public class DAOFactory {
	public static final String SUPPLIER_DAO = "supplierdao";
	public static final String PRODUCT_DAO = "productdao";
	private static final DAOFactory instance = new DAOFactory();
	private Map<String, Object> daoCache = new HashMap<String, Object>();

	private Properties p;

	private DAOFactory() {
		try {
			p = new Properties();
			p.load(this.getClass().getClassLoader()
					.getResourceAsStream("dao.properties"));
			for (Entry<Object, Object> entry : p.entrySet()) {
				String name = entry.getKey().toString();
				String clzzName = entry.getValue().toString();
				Object inst = Class.forName(clzzName).newInstance();
				daoCache.put(name, inst);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
//	public <T> T getDAO(String name,Class<T> clzz){
//		return (T)daoCache.get(name);
//	}

	public Object getDAO(String name) {
		return daoCache.get(name);
	}

	// public Object getDAO(String name) {
	// try {
	// String clzzName = p.getProperty(name);
	// return Class.forName(clzzName).newInstance();
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// return null;
	// }

	public static DAOFactory getInstance() {
		return instance;
	}

	// public IProductDAO getProductDao() {
	// return new ProductDAOJdbcImpl();
	// }
	//
	// public ISupplierDAO getSupplierDao() {
	// return new SupplierDAOJdbcImpl();
	// }

	// public Object getDAO(String name) {
	// if (SUPPLIER_DAO.equals(name)) {
	// return new SupplierDAOJdbcImpl();
	// } else if (PRODUCT_DAO.equals(name)) {
	// return new ProductDAOJdbcImpl();
	// }
	// return null;
	// }

}
